library(survey)
library(foreign)
library(ggplot2)
library(cowplot)
library(dplyr)
library(egg)
library(ggpubr)
library(gridGraphics)

## see wave2_CLEANING.R for data cleaning
load("~/Dropbox/Public_Conf_Mil/Data_and_code/wave2_data/clean_dataw2.RData")

## Create weighted survey design object
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

addline_format <- function(x,...){
  gsub('\\s','\n',x)
}

#### TREATMENT EFFECTS FOR PUBLIC CONFIDENCE ####

fig71_means <- svyby(~Q11_d, ~P_ASSIGN1, w2_design, svymean, na.rm = TRUE)[c(1,6,7,8),]

fig71_df <- data.frame(conf = fig71_means[,2] * 100,
                       ci_lo = (fig71_means[,2] - 1.96*fig71_means[,3]) * 100,
                       ci_hi = (fig71_means[,2] + 1.96*fig71_means[,3]) * 100,
                       cond = c(1:4))

fig71_plot <- ggplot(fig71_df) +
  geom_hline(yintercept = fig71_df$conf[1], colour = "red", lty = 1) +
  geom_pointrange(aes(x = cond, y = conf, ymin = ci_lo, ymax = ci_hi), 
                  fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = conf, label = sprintf("%0.2f", round(conf, digits = 2))),
            size = 3, vjust = 2.5) +
  xlab("Treatment Condition") +
  scale_x_continuous(breaks = c(1:4),
                     labels = addline_format(c("Control", "Republican Military", 
                                               "Democratic Military","Partisan Military"))) +
  ylab("Respondent Agreement (%)") +
  theme_bw()
fig71_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_treateffects.jpeg", 
       plot = fig71_plot, dpi = 1000)

#### TREATMENT EFFECTS BY PARTY ####

fig72_means <- svyby(~Q11_d, ~P_ASSIGN1+party, w2_design, svymean, 
                     na.rm = TRUE)[c(1,6,7,8,9,14,15,16,17,22,23,24),]

fig72_df <- data.frame(dem = fig72_means[1:4,3] * 100,
                       dem_lo = (fig72_means[1:4,3] - 1.96*fig72_means[1:4,4]) * 100,
                       dem_hi = (fig72_means[1:4,3] + 1.96*fig72_means[1:4,4]) * 100,
                       ind = fig72_means[5:8,3] * 100,
                       ind_lo = (fig72_means[5:8,3] - 1.96*fig72_means[5:8,4]) * 100,
                       ind_hi = (fig72_means[5:8,3] + 1.96*fig72_means[5:8,4]) * 100,
                       rep = fig72_means[9:12,3] * 100,
                       rep_lo = (fig72_means[9:12,3] - 1.96*fig72_means[9:12,4]) * 100,
                       rep_hi = (fig72_means[9:12,3] + 1.96*fig72_means[9:12,4]) * 100,
                       dcond = c(1,5,9,13),
                       icond = c(2,6,10,14),
                       rcond = c(3,7,11,15))

fig72_plot <- ggplot(fig72_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Respondent Agreement (%)") +
  #geom_hline(yintercept = 0, colour = "black", lty = 1) +
  scale_x_continuous(breaks = c(2, 6, 10, 14),
                     labels = addline_format(c("Control", "Republican Military", 
                                               "Democratic Military",
                                               "Partisan Military"))) +
  theme_bw()
fig72_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_effects_party.jpeg", 
       plot = fig72_plot, dpi = 1000)

#### MILITARY CUEING AND VOTE CHOICE ####

fig73_means <- svyby(~Q36_trump, ~P_ASSIGN5, w2_design, svymean, na.rm = TRUE)

fig73_df <- data.frame(pct = fig73_means[,2] * 100,
                       ci_lo = (fig73_means[,2] - 1.96*fig73_means[,3]) * 100,
                       ci_hi = (fig73_means[,2] + 1.96*fig73_means[,3]) * 100,
                       cond = c(1:5))

fig73_plot <- ggplot(fig73_df) +
  #geom_hline(yintercept = fig71_df$conf[1], colour = "red", lty = 1) +
  geom_pointrange(aes(x = cond, y = pct, ymin = ci_lo, ymax = ci_hi), 
                  fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = pct, label = sprintf("%0.2f", round(pct, digits = 2))),
            size = 3, vjust = 2.5) +
  xlab("Treatment Condition") +
  scale_x_continuous(breaks = c(1:5),
                     labels = addline_format(c("Control", 
                                               "Military: Biden",
                                               "Military: Trump",
                                               "Generals: Biden",
                                               "Generals: Trump"))) +
  ylab("Support for Trump (%)") +
  theme_bw()
fig73_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_trumpsupport.jpeg", 
       plot = fig73_plot, dpi = 1000)

#### CUEING AND VOTE CHOICE BY PARTY ####

fig74_means <- svyby(~Q36_trump, ~P_ASSIGN5+party, w2_design, svymean, na.rm = TRUE)

fig74_df <- data.frame(dem = fig74_means[1:5,3] * 100,
                       dem_lo = (fig74_means[1:5,3] - 1.96*fig74_means[1:5,4]) * 100,
                       dem_hi = (fig74_means[1:5,3] + 1.96*fig74_means[1:5,4]) * 100,
                       ind = fig74_means[6:10,3] * 100,
                       ind_lo = (fig74_means[6:10,3] - 1.96*fig74_means[6:10,4]) * 100,
                       ind_hi = (fig74_means[6:10,3] + 1.96*fig74_means[6:10,4]) * 100,
                       rep = fig74_means[11:15,3] * 100,
                       rep_lo = (fig74_means[11:15,3] - 1.96*fig74_means[11:15,4]) * 100,
                       rep_hi = (fig74_means[11:15,3] + 1.96*fig74_means[11:15,4]) * 100,
                       dcond = c(1,5,9,13,17),
                       icond = c(2,6,10,14,18),
                       rcond = c(3,7,11,15,19))

fig74_plot <- ggplot(fig74_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Support for Trump (%)") +
  #geom_hline(yintercept = 0, colour = "black", lty = 1) +
  scale_x_continuous(breaks = c(2,6,10,14,18),
                     labels = addline_format(c("Control", 
                                               "Military: Biden",
                                               "Military: Trump",
                                               "Generals: Biden",
                                               "Generals: Trump"))) +
  theme_bw()
fig74_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_trump_party.jpeg", 
       plot = fig74_plot, dpi = 1000)

#### TREATMENT EFFECTS FOR INSURRECTION ACT BY PARTY ####

fig75_means <- svyby(~Q25A_d, ~P_ASSIGN3A+party, w2_design, svymean, na.rm = TRUE)

fig75_df <- data.frame(dem = fig75_means[1:3,3] * 100,
                       dem_lo = (fig75_means[1:3,3] - 1.96*fig75_means[1:3,4]) * 100,
                       dem_hi = (fig75_means[1:3,3] + 1.96*fig75_means[1:3,4]) * 100,
                       rep = fig75_means[7:9,3] * 100,
                       rep_lo = (fig75_means[7:9,3] - 1.96*fig75_means[7:9,4]) * 100,
                       rep_hi = (fig75_means[7:9,3] + 1.96*fig75_means[7:9,4]) * 100,
                       dcond = c(1,4,7),
                       rcond = c(2,5,8))

fig75_plot <- ggplot(fig75_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Support for Insurrection Act (%)") +
  #geom_hline(yintercept = 0, colour = "black", lty = 1) +
  scale_x_continuous(breaks = c(1.5,4.5,7.5),
                     labels = addline_format(c("Control", 
                                               "Oppose",
                                               "Support"))) +
  theme_bw()
fig75_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_insurrectionact.jpeg", 
       plot = fig75_plot, dpi = 1000)

#### SUPPORT FOR CRITICIZING POTUS BY PARTY ####

fig76_means <- svyby(~ Q20DD_d, ~ P_Q20+party, w2_design, svymean, na.rm = TRUE)

fig76_df <- data.frame(dem = fig76_means[1:3,3] * 100,
                       dem_lo = (fig76_means[1:3,3] - 1.96*fig76_means[1:3,4]) * 100,
                       dem_hi = (fig76_means[1:3,3] + 1.96*fig76_means[1:3,4]) * 100,
                       ind = fig76_means[4:6,3] * 100,
                       ind_lo = (fig76_means[4:6,3] - 1.96*fig76_means[4:6,4]) * 100,
                       ind_hi = (fig76_means[4:6,3] + 1.96*fig76_means[4:6,4]) * 100,
                       rep = fig76_means[7:9,3] * 100,
                       rep_lo = (fig76_means[7:9,3] - 1.96*fig76_means[7:9,4]) * 100,
                       rep_hi = (fig76_means[7:9,3] + 1.96*fig76_means[7:9,4]) * 100,
                       dcond = c(1,5,9),
                       icond = c(2,6,10),
                       rcond = c(3,7,11))

fig76_plot <- ggplot(fig76_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Respondent Agreement (%)") +
  #geom_hline(yintercept = 0, colour = "black", lty = 1) +
  scale_x_continuous(breaks = c(2,6,10),
                     labels = addline_format(c("Criticize Trump",
                                               "Criticize Obama",
                                               "Criticize POTUS"))) +
  theme_bw()
fig76_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_criticize.jpeg", 
       plot = fig76_plot, dpi = 1000)

#### CRITICIZING POTUS BY PARTY AND CONFIDENCE ####

fig77_means <- svyby(~ Q20DD_d, ~ P_Q20+party+Q12_d, w2_design, svymean, na.rm = TRUE)

fig77_df1 <- data.frame(dem = fig77_means[1:3,4] * 100,
                        dem_lo = (fig77_means[1:3,4] - 1.96*fig77_means[1:3,5]) * 100,
                        dem_hi = (fig77_means[1:3,4] + 1.96*fig77_means[1:3,5]) * 100,
                        ind = fig77_means[4:6,4] * 100,
                        ind_lo = (fig77_means[4:6,4] - 1.96*fig77_means[4:6,5]) * 100,
                        ind_hi = (fig77_means[4:6,4] + 1.96*fig77_means[4:6,5]) * 100,
                        rep = fig77_means[7:9,4] * 100,
                        rep_lo = (fig77_means[7:9,4] - 1.96*fig77_means[7:9,5]) * 100,
                        rep_hi = (fig77_means[7:9,4] + 1.96*fig77_means[7:9,5]) * 100,
                        dcond = c(1,5,9),
                        icond = c(2,6,10),
                        rcond = c(3,7,11))

fig77_df2 <- data.frame(dem = fig77_means[10:12,4] * 100,
                        dem_lo = (fig77_means[10:12,4] - 1.96*fig77_means[10:12,5]) * 100,
                        dem_hi = (fig77_means[10:12,4] + 1.96*fig77_means[10:12,5]) * 100,
                        ind = fig77_means[13:15,4] * 100,
                        ind_lo = (fig77_means[13:15,4] - 1.96*fig77_means[13:15,5]) * 100,
                        ind_hi = (fig77_means[13:15,4] + 1.96*fig77_means[13:15,5]) * 100,
                        rep = fig77_means[16:18,4] * 100,
                        rep_lo = (fig77_means[16:18,4] - 1.96*fig77_means[16:18,5]) * 100,
                        rep_hi = (fig77_means[16:18,4] + 1.96*fig77_means[16:18,5]) * 100,
                        dcond = c(1,5,9),
                        icond = c(2,6,10),
                        rcond = c(3,7,11))
 
fig77_plot1 <- ggplot(fig77_df1) + ## Not Confident
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 2, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 2, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 2, vjust = 2) +
  xlab("") + ylab("Respondent Agreement (%)") + ylim(0,90) +
  ggtitle("Not Confident") +
  scale_x_continuous(breaks = c(2,6,10),
                     labels = addline_format(c("Criticize Trump",
                                               "Criticize Obama",
                                               "Criticize POTUS"))) +
  theme_bw()
fig77_plot1

fig77_plot2 <- ggplot(fig77_df2) + ## Confident
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 2, vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), 
            size = 2, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 2, vjust = 2) +
  xlab("") + ylab("Respondent Agreement (%)") + ylim(0,90) +
  ggtitle("Confident") +
  scale_x_continuous(breaks = c(2,6,10),
                     labels = addline_format(c("Criticize Trump",
                                               "Criticize Obama",
                                               "Criticize POTUS"))) +
  theme_bw()
fig77_plot2

fig77_plot <- ggarrange(fig77_plot1 + theme(axis.text.y = element_text(size = 6),
                                            plot.title = element_text(size = 11),
                                            axis.title.y = element_text(size = 8),
                                            axis.text.x = element_text(size = 8),
                                            axis.title.x = element_text(size = 8)), 
                        fig77_plot2 + theme(axis.text.y = element_text(size = 6),
                                            plot.title = element_text(size = 11),
                                            axis.title.y = element_text(size = 8),
                                            axis.text.x = element_text(size = 8),
                                            axis.title.x = element_text(size = 8)),
                        nrow = 1, ncol = 2)
fig77_plot
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 7 Figures/wave2_criticize_conf.jpeg", 
       plot = fig77_plot, dpi = 1000)
